
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using AnovoAndes.Framework.Utilidades;
using NovaSystem.EntidadesNegocio;

namespace NovaSystem.AccesoDatos
{
	public class proveedorDAO 
	{
			
		#region [Variables Publicas]
        List<string> listaEsquema = new List<string>();
		System.Configuration.AppSettingsReader read = new System.Configuration.AppSettingsReader();
        #endregion
		
		#region Constructores
		public proveedorDAO()
		{
		}
		#endregion
		
		#region Metodos Privados
		
		 private List<string> LlenarEsquema(IDataReader dr)
        {
            DataTable schemaTable;
            schemaTable = dr.GetSchemaTable();
            listaEsquema = new List<string>();
            const int columnaNombre = 0;
            //Para cada campo de la tabla...
            foreach (DataRow myField in schemaTable.Rows)
            {
                //Agrega nombre del campo a la lista generica.
                listaEsquema.Add(myField[columnaNombre].ToString());
            }
            return listaEsquema;

        }
		
		private proveedor Crear(IDataReader dr)
		{
			proveedor vObj = new proveedor();
			try
			{
				listaEsquema.Sort();
							if (listaEsquema.BinarySearch("idProveedor",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["idProveedor"] is System.DBNull))
					{
						vObj.idProveedor= (int)dr["idProveedor"];
					}
				}
				if (listaEsquema.BinarySearch("rutProveedor",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["rutProveedor"] is System.DBNull))
					{
						vObj.rutProveedor= (string)dr["rutProveedor"];
					}
				}
				if (listaEsquema.BinarySearch("nombreProveedor",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["nombreProveedor"] is System.DBNull))
					{
						vObj.nombreProveedor= (string)dr["nombreProveedor"];
					}
				}
				if (listaEsquema.BinarySearch("direccionProveedor",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["direccionProveedor"] is System.DBNull))
					{
						vObj.direccionProveedor= (string)dr["direccionProveedor"];
					}
				}
				if (listaEsquema.BinarySearch("conceptoPais",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["conceptoPais"] is System.DBNull))
					{
						vObj.conceptoPais= (string)dr["conceptoPais"];
					}
				}
				if (listaEsquema.BinarySearch("codigoPais",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["codigoPais"] is System.DBNull))
					{
						vObj.codigoPais= (string)dr["codigoPais"];
					}
				}
				if (listaEsquema.BinarySearch("Ciudad",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["Ciudad"] is System.DBNull))
					{
						vObj.Ciudad= (string)dr["Ciudad"];
					}
				}
				if (listaEsquema.BinarySearch("Provincia",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["Provincia"] is System.DBNull))
					{
						vObj.Provincia= (string)dr["Provincia"];
					}
				}
				if (listaEsquema.BinarySearch("contacto",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["contacto"] is System.DBNull))
					{
						vObj.contacto= (string)dr["contacto"];
					}
				}
				if (listaEsquema.BinarySearch("telefono",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["telefono"] is System.DBNull))
					{
						vObj.telefono= (string)dr["telefono"];
					}
				}
				if (listaEsquema.BinarySearch("email",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["email"] is System.DBNull))
					{
						vObj.email= (string)dr["email"];
					}
				}
				if (listaEsquema.BinarySearch("website",StringComparer.OrdinalIgnoreCase)>-1)
				{
					if(!(dr["website"] is System.DBNull))
					{
						vObj.website= (string)dr["website"];
					}
				}

				return vObj;
			}
			catch(Exception ex)
			{
				throw new Exception(ex.Message);
			}
		}
		
		private List<proveedor> CrearDTOs(IDataReader dr) 
		{
			List<proveedor> listaDTO = new List<proveedor>();
			if 	(dr != null)
			{
				listaEsquema = LlenarEsquema(dr);// Validadores.LlenarEsquema(dr);
				
				while (dr.Read())
				{
					listaDTO.Add(Crear(dr));
				}
				dr.Close();
			}
			return listaDTO;
		}
		
		#endregion
		
		#region Metodos Publicos
        public List<proveedor> Buscar(proveedor dto)
        {
            MySqlConnection conn = new MySqlConnection();
            List<proveedor> lista = new List<proveedor>();
            try
            {
                string connString = read.GetValue("Database", typeof(string)).ToString();
                conn.ConnectionString = connString;
                conn.Open();
                MySqlCommand objDbCommand = new MySqlCommand();
                objDbCommand.Connection = conn;
                objDbCommand.CommandType = CommandType.StoredProcedure;
                objDbCommand.CommandText = "usp_S_buscaProveedor";
                objDbCommand.Parameters.AddWithValue("_rutProveedor", AssertNull.getValue(dto.rutProveedor));
                objDbCommand.Parameters.AddWithValue("_nombreProveedor", AssertNull.getValue(dto.nombreProveedor));
                objDbCommand.Parameters.AddWithValue("_codigoPais", AssertNull.getValue(dto.codigoPais));
                objDbCommand.Parameters.AddWithValue("_contacto", AssertNull.getValue(dto.contacto));
                objDbCommand.Parameters.AddWithValue("_email", AssertNull.getValue(dto.email));
                using (IDataReader objDataReader = objDbCommand.ExecuteReader())
                {
                    lista = CrearDTOs(objDataReader);
                }
                CerrarConn(conn);
                return lista;
            }
            catch (Exception ex)
            {
                CerrarConn(conn);
                throw new Exception(ex.Message);
            }
        }


		public List<proveedor> Listar()
		{
			MySqlConnection conn = new MySqlConnection();
			List<proveedor> lista = new List<proveedor>();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_s_proveedor";
				
				using (IDataReader objDataReader = objDbCommand.ExecuteReader())
				{
					lista = CrearDTOs(objDataReader);
				}
				CerrarConn(conn);
				return lista;
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		
		private void CerrarConn(MySqlConnection conn)
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }
		
		public string Insertar( proveedor dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_I_proveedor";
				objDbCommand.Parameters.AddWithValue("_idProveedor",AssertNull.getValue(dto.idProveedor));
				objDbCommand.Parameters.AddWithValue("_rutProveedor",AssertNull.getValue(dto.rutProveedor));
				objDbCommand.Parameters.AddWithValue("_nombreProveedor",AssertNull.getValue(dto.nombreProveedor));
				objDbCommand.Parameters.AddWithValue("_direccionProveedor",AssertNull.getValue(dto.direccionProveedor));
				objDbCommand.Parameters.AddWithValue("_conceptoPais",AssertNull.getValue(dto.conceptoPais));
				objDbCommand.Parameters.AddWithValue("_codigoPais",AssertNull.getValue(dto.codigoPais));
				objDbCommand.Parameters.AddWithValue("_Ciudad",AssertNull.getValue(dto.Ciudad));
				objDbCommand.Parameters.AddWithValue("_Provincia",AssertNull.getValue(dto.Provincia));
				objDbCommand.Parameters.AddWithValue("_contacto",AssertNull.getValue(dto.contacto));
				objDbCommand.Parameters.AddWithValue("_telefono",AssertNull.getValue(dto.telefono));
				objDbCommand.Parameters.AddWithValue("_email",AssertNull.getValue(dto.email));
				objDbCommand.Parameters.AddWithValue("_website",AssertNull.getValue(dto.website));

				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
				return "";
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		public void Eliminar(proveedor dto)
		{
			MySqlConnection conn = new MySqlConnection();
			try
			{
				string connString = read.GetValue("Database",typeof(string)).ToString();
				conn.ConnectionString = connString;
				conn.Open();
				MySqlCommand objDbCommand = new MySqlCommand();
				objDbCommand.Connection = conn;
				objDbCommand.CommandType = CommandType.StoredProcedure;
				objDbCommand.CommandText = "usp_D_proveedor";
								objDbCommand.Parameters.AddWithValue("_idProveedor",AssertNull.getValue(dto.idProveedor));

				objDbCommand.ExecuteNonQuery();
				CerrarConn(conn);
			}
			catch(Exception ex)
			{
				CerrarConn(conn);
				throw new Exception(ex.Message);
			}
		}
		
		#endregion
		
	}
}
